package nero.yncc.com.createart.activity;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.base.BaseActivity;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.utils.HttpUtils;
import nero.yncc.com.createart.utils.TextUtil;

import static nero.yncc.com.createart.base.BaseApplication.time;

/**
 * Created by nero on 2017/7/8.
 */
@ContentView(R.layout.activity_register)
public class ActivityRegister extends BaseActivity {
    @ViewInject(R.id.et_account)
    EditText et_account;
    @ViewInject(R.id.et_password)
    EditText et_password;
    @ViewInject(R.id.et_password_again)
    EditText et_password_again;
    @ViewInject(R.id.et_code)
    EditText et_code;
    @ViewInject(R.id.tv_send)
    TextView tv_send;
    @ViewInject(R.id.btn_submit)
    TextView btn_submit;

    Timer timer;

    @Override
    protected void initView() {
        super.initView();
        setTitleBarTitle("注册");
        timer = new Timer();
        if (time > 0) {
            canSend = false;
            tv_send.setEnabled(false);
            tv_send.setTextColor(getResources().getColor(R.color.textColorGray_low));
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            try {
                                time--;
                                if (time <= 0) {
                                    tv_send.setEnabled(true);
                                    tv_send.setTextColor(getResources().getColor(R.color.textColorGray));
                                    tv_send.setText("再次发送");
                                    cancel();
                                } else {
                                    tv_send.setText(time + "秒");
                                }

                            } catch (Exception e) {
                            }
                        }
                    });
                }
            }, 0, 1000);

        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        tv_send.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
    }


    boolean canSend = true;
    String code = "";
    String phone = "";
    String pwd1="";
    String pwd2="";

    @Override
    protected void doClick(int id) {
        super.doClick(id);
        switch (id) {
            case R.id.tv_send:
                phone = et_account.getText().toString();
                if (TextUtils.isEmpty(phone) || phone.length() != 11) {
                    showToast("请输入正确的手机号码");
                } else {
                    //发送验证码
                    HashMap<String, String> hashMap = new HashMap<>();
                    hashMap.put("mobile", phone);
                    hashMap.put("check_exists", "true");
                    new HttpUtils().post(this, Constants.API_SEND_CODE, hashMap, null, HttpUtils.TYPE_NOTHING, new HttpUtils.HttpCallback() {
                        @Override
                        public void success(String msg, Object json) {
                            showToast(msg);
                            if (canSend) {
                                canSend = false;
                                tv_send.setEnabled(false);
                                tv_send.setTextColor(getResources().getColor(R.color.textColorGray_low));
                                time = 60;
                                timer.schedule(new TimerTask() {
                                    @Override
                                    public void run() {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                try {
                                                    time--;
                                                    if (time <= 0) {
                                                        tv_send.setEnabled(true);
                                                        tv_send.setTextColor(getResources().getColor(R.color.textColorGray));
                                                        tv_send.setText("再次发送");
                                                        cancel();
                                                    } else {
                                                        tv_send.setText(time + "秒");
                                                    }

                                                } catch (Exception e) {
                                                }
                                            }
                                        });

                                    }
                                }, 1000, 1000);
                            }
                        }

                        @Override
                        public void fail(String failMsg) {
                            showToast(failMsg);
                        }
                    });
                }


                break;
            case R.id.btn_submit:
                register();
                break;
        }
    }

    private void register() {
        phone=et_account.getText().toString();
        pwd1=et_password.getText().toString();
        pwd2=et_password_again.getText().toString();
        code=et_code.getText().toString();
        if(TextUtils.isEmpty(phone) || phone.length()!=11){
            showToast("请输入正确的手机号");
            return;
        }
        if(TextUtils.isEmpty(pwd1)){
            showToast("请输入密码");
            return;
        }
        if(TextUtils.isEmpty(pwd2)){
            showToast("请输入确认密码");
            return;
        }
        if(!pwd1.equals(pwd2)){
            showToast("两次密码不一致");
            return;
        }
        if(TextUtils.isEmpty(code)){
            showToast("请输入验证码");
            return;
        }

        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("mobile",phone);
        hashMap.put("password",pwd1);
        hashMap.put("captcha",code);
        new HttpUtils().post(this, Constants.API_REGISTER, hashMap, null, HttpUtils.TYPE_NOTHING, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                showToast(msg);
                finish();
            }

            @Override
            public void fail(String failMsg) {
                showToast(failMsg);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
